package com.kyrie.lib_sys.application.autorApplys.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kyrie.lib_sys.common.entity.PageResult;
import com.kyrie.lib_sys.common.pojo.dto.AuthorDTO;
import com.kyrie.lib_sys.common.pojo.dto.AuthorPageQueryDTO;
import com.kyrie.lib_sys.common.pojo.entity.Author;
import com.kyrie.lib_sys.application.autorApplys.mapper.AuthorMapper;
import com.kyrie.lib_sys.application.autorApplys.service.AuthorService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者service类
 *
 * @author heefM
 * @date 2025-08-09
 */
@Service
public class AuthorServiceImpl implements AuthorService {

    @Autowired
    private AuthorMapper authorMapper;

    /**
     * 添加作者
     *
     * @param authorDTO
     * @return
     */
    @Override
    public Author insert(AuthorDTO authorDTO) {
        Author author = new Author();
        BeanUtils.copyProperties(authorDTO, author);
        int count = authorMapper.insert(author);
        return count > 0 ? author : null;
    }

    /**
     * 查询作者
     *
     * @param authorDTO
     * @return
     */
    @Override
    public Author queryAuthor(AuthorDTO authorDTO) {
        Author author = new Author();
        BeanUtils.copyProperties(authorDTO, author);
        return authorMapper.queryByCondition(author);
    }

    /**
     * 根据ID查询作者
     *
     * @param id
     * @return
     */
    @Override
    public AuthorDTO queryAuthorById(Long id) {
        Author author = authorMapper.queryById(id);
        if (author != null) {
            AuthorDTO authorDTO = new AuthorDTO();
            BeanUtils.copyProperties(author, authorDTO);
            return authorDTO;
        }
        return null;
    }

    /**
     * 查询作者列表
     *
     * @param authorPageQueryDTO
     * @return
     */
    @Override
    public PageResult queryList(AuthorPageQueryDTO authorPageQueryDTO) {
        //1.设置默认起始分页
        PageHelper.startPage(authorPageQueryDTO.getPageNum(), authorPageQueryDTO.getPageSize());

        //2.查询作者列表
        Page<Author> page = authorMapper.queryList(authorPageQueryDTO);

        //3.获取结果
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * 分页查询作者
     *
     * @param authorPageQueryDTO
     * @return
     */
    @Override
    public PageResult queryPage(AuthorPageQueryDTO authorPageQueryDTO) {
        //1.设置默认起始分页
        PageHelper.startPage(authorPageQueryDTO.getPageNum(), authorPageQueryDTO.getPageSize());

        //2.查询作者列表
        Page<Author> page = authorMapper.queryList(authorPageQueryDTO);

        //3.获取结果
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * 条件查询作者
     *
     * @param authorDTO
     * @return
     */
    @Override
    public AuthorDTO queryByCondition(AuthorDTO authorDTO) {
        Author author = new Author();
        BeanUtils.copyProperties(authorDTO, author);
        Author result = authorMapper.queryByCondition(author);
        if (result != null) {
            AuthorDTO resultDTO = new AuthorDTO();
            BeanUtils.copyProperties(result, resultDTO);
            return resultDTO;
        }
        return null;
    }

    /**
     * 更新作者
     *
     * @param authorDTO
     */
    @Override
    public void update(AuthorDTO authorDTO) {
        Author author = new Author();
        BeanUtils.copyProperties(authorDTO, author);
        authorMapper.update(author);
    }

    /**
     * 删除作者
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        authorMapper.deleteById(id);
    }

    /**
     * 批量删除作者
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        authorMapper.deleteBatch(ids);
    }

    /**
     * 批量插入作者
     *
     * @param authorDTOS
     */
    @Override
    public void insertBatch(List<AuthorDTO> authorDTOS) {
        ArrayList<Author> authorList = new ArrayList<>();
        for (AuthorDTO authorDTO : authorDTOS) {
            if (authorDTO != null){
                Author author = new Author();
                BeanUtils.copyProperties(authorDTO, author);
                authorList.add(author);
            }
        }
        authorMapper.insertBatch(authorList);
    }
}
